#include <iostream>
#include "weapon.h"

using namespace std;

Weapon::Weapon(MD2Model *_weaponModel)
{
	weaponModel = _weaponModel;

	ammo = 10;
	//default frames names
	strcpy_s(shootFrame, strlen(shootFrame), "pow");
	strcpy_s(swayFrame, strlen(swayFrame), "sway");
	strcpy_s(hideFrame, strlen(hideFrame), "putway");
	strcpy_s(pickFrame, strlen(pickFrame), "active");

	movingAnimationSpeed = 0.05;
	shootingAnimationSpeed = 0.035;
	hidingAnimationSpeed = 0.1;
	pickingAnimationSpeed = 0.1;

	weaponModel->setAnimation(swayFrame);

	isShooting = false;
	isHiding = false;
	isPicking = false;
}

Weapon::~Weapon()
{
}

int Weapon::getAmmo()
{
	return ammo;
}

void Weapon::shoot()
{
	if(!isShooting)
	{
		--ammo;
		isShooting = true;
		weaponModel->setFinishedAnimation(false);
		weaponModel->setAnimation(shootFrame, swayFrame, true);
	}
}

void Weapon::hideWeapon()
{
	if(!isShooting)
	{
		isHiding = true;
		weaponModel->setFinishedAnimation(false);
		weaponModel->setAnimation(hideFrame, pickFrame, true);
	}
}

void Weapon::pickWeapon()
{
	if(!isShooting)
	{
		isPicking = true;
		weaponModel->setFinishedAnimation(false);
		weaponModel->setAnimation(pickFrame, swayFrame, true);
	}
}

void Weapon::draw()
{
	weaponModel->draw();
}


void Weapon::advanceAnimation(bool isOnTheGround, bool moves)
{
	if (isShooting)
		weaponModel->advance(shootingAnimationSpeed);
	else if (isPicking)
		weaponModel->advance(pickingAnimationSpeed);
	else if(isHiding)
		weaponModel->advance(hidingAnimationSpeed);
	else if(isOnTheGround && moves)
			weaponModel->advance(movingAnimationSpeed);
}

bool Weapon::getFinishedAnimation()
{
	return weaponModel->getFinishedAnimation();
}

void Weapon::setIsPicking(bool value)
{
	weaponModel->setAnimation(pickFrame, swayFrame, true);
	weaponModel->setAnimation(hideFrame, swayFrame, true);
}

void Weapon::resetActions()
{
	isShooting = false;
	isPicking = false;
	isHiding = false;
}

void Weapon::setAnimation(char* name, char *frameToReturn, bool returnToPreviousFrame)
{
	weaponModel->setAnimation(name, frameToReturn, returnToPreviousFrame);
}

bool Weapon::getIsShooting()
{
	return isShooting;
}